package de.tabuma.pm.projectnavigation.controller;

import de.tabuma.pm.project.entity.Project;
import de.tabuma.pm.projectnavigation.entity.ProjectNavigation;
import de.tabuma.pm.projectnavigation.entity.ProjectNavigationWrapper;
import de.tabuma.pm.projectnavigation.logik.ProjectNavigationFacade;
import de.tabuma.pm.*;
import de.tabuma.lib.tabumacontroller;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class update extends tabumacontroller {

    private static final Logger log = Logger.getLogger(update.class.getName());

    public update(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    public void updateList(HttpServletRequest request) {

        Project project = (Project) request.getAttribute(constants.paramProject);

        List<ProjectNavigation> pnList = ProjectNavigationFacade.getList(project.getId().intValue());

        ProjectNavigation pn = new ProjectNavigation();
        pn.setId(new Long(0));
        pn.setProjectId(project.getId().intValue());
        pn.setNavMode(0);

        ProjectNavigationWrapper pnw = new ProjectNavigationWrapper(pn, -1);
        pnw.loadSubElement(pnList);
        request.setAttribute(constants.ProjectNavigationWrapper,pnw);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        if(request.getParameter(constants.ProjectNavigationLevelMinus)!=null) {

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            String navid = (String) request.getParameter(constants.ProjectNavigationId);
            int navidInt = Integer.valueOf(navid);
            
            ProjectNavigation pn = ProjectNavigationFacade.get(navidInt);
            pn.setOrder(pn.getOrder()+1);
            
            ProjectNavigationFacade.update(pn, user.getEmail());

            updateList(request);
        }

        if(request.getParameter(constants.ProjectNavigationLevelPlus)!=null) {

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            String navid = (String) request.getParameter(constants.ProjectNavigationId);
            int navidInt = Integer.valueOf(navid);

            ProjectNavigation pn = ProjectNavigationFacade.get(navidInt);
            pn.setOrder(pn.getOrder()-1);
            ProjectNavigationFacade.update(pn, user.getEmail());

            updateList(request);
        }

        if(request.getParameter(constants.ProjectNavigationUpdate)!=null) {

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            request.setAttribute(constants.ProjectNavigationUpdate, "true");
            String navid = (String) request.getParameter(constants.ProjectNavigationId);
            int navidInt = Integer.valueOf(navid);
            ProjectNavigation pn = ProjectNavigationFacade.get(navidInt);
            request.setAttribute(constants.ProjectNavigation, pn);

            Project project = (Project) request.getAttribute(constants.paramProject);

            List<ProjectNavigation> pnList = ProjectNavigationFacade.getList(project.getId().intValue());
            List<ProjectNavigation> pnList1 = ProjectNavigationFacade.getSubnavigationList(pn.getId().intValue());

            for(int i=0;i<pnList.size();i++) {
                ProjectNavigation pn1 = pnList.get(i);
                if(pn1.getId().intValue()==pn.getId().intValue()) {
                    pnList.remove(i);
                    i--;
                }
                for(int j=0;j<pnList1.size();j++) {
                    ProjectNavigation pn2 = pnList1.get(j);
                    if(pn1.getId().intValue()==pn2.getId().intValue()) {
                        pnList.remove(i);
                        i--;
                    }
                }
            }

            request.setAttribute(constants.ProjectNavigationList, pnList);

        }

        if(request.getParameter(constants.ProjectNavigationUpdate1)!=null) {

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            String name = (String) request.getParameter(constants.ProjectNavigationName);
            String navid = (String) request.getParameter(constants.ProjectNavigationId);
            int navidInt = Integer.valueOf(navid).intValue();
            String navsubid = (String) request.getParameter(constants.ProjectNavigationHeadId);
            int navsubidInt = Integer.valueOf(navsubid).intValue();

            ProjectNavigation pn = ProjectNavigationFacade.get(navidInt);
            pn.setNavigationId(navsubidInt);
            pn.setName(name);
            ProjectNavigationFacade.update(pn, user.getEmail());

            updateList(request);
        }

    }

}
